import UploadS from '../../../../components/upload-s';

export default function () {
    return {
        components: {
            UploadS,
        },
        props: {
            type: {
                type: String,
                default: '',
            },
            data: {
                type: Object,
                default: () => ({}),
            },
        },
        data() {
            var that = this;
    
            return {
                form: {
                    fileguid: '',
                    filetype: '',
                    filetypename: '',
                    orgcode: '',
                    wjstarttime: '',
                    wjendtime: '',
                    name: '',
                    fileid: '',
                },
    
                uploadExtra: {
                    newtypecode: 'yunpan',
                    typecode: that.type,
                },

                submitController: false,
            };
        },
        computed: {
            dateRange: {
                get() {
                    return [
                        this.form.wjstarttime || '',
                        this.form.wjendtime || '',
                    ];
                },
                set(val) {
                    val = val || [];
    
                    this.form.wjstarttime = val[0] || '';
                    this.form.wjendtime = val[1] || '';
                },
            },
            detailExtra() {
                return {
                    rowguid: this.data.rowguid,
                };
            },
        },
        methods: {
            submit() {
                if (!this.$refs.upload.exist) {
                    showMsgBox('请上传文件');
                    return;
                }

                if (this.submitController) {
                    // 已经提交中
                    return;
                }

                this.submitController = true;
    
                var form = this.$refs.form;
    
                if (form) {
                    form.onSubmit();
                } else {
                    this.submitHandler();
                }
            },
            close() {
                this.$emit('close');
            },
            submitHandler() {
                this.$post(`${this.$store.state.api.sysUrl}/file/bidding/plan/edit.json`, {
                    ...this.form,
                    typecode: this.type,
                    fileid: this.form.fileguid,
                }, data => {
                    this.close();
                    this.submitController = false;
                });
            },
            detailHandler() {
                this.$get(`${this.$store.state.api.sysUrl}/file/bidding/plan/detail.json`, {
                    rowguid: this.data.rowguid,
                }, data => {
                    var form = data.ext;
                    
                    form.fileguid = data.file.fileguid;
                    form.orgcode = form.orgcode || '';
                    form.wjstarttime = form.wjstarttime || '';
                    form.wjendtime = form.wjendtime || '';
    
                    this.form = form;
                });
            },
            fileExist() {
                return this.$refs.upload.exist;
            },
        },
        mounted() {
            this.detailHandler();
        },
    }
}